package com.kingtsoft.study.common;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.kingtsoft.study.constant.ParamConstants;
import com.kingtsoft.study.model.StudyStudent;
import com.kingtsoft.study.model.StudyTeacher;
import com.kingtsoft.study.model.StudyUser;

/**
* @ClassName: ContextHolderUtils 
* @Description: 上下文工具类，用于在Server,Dao层获取当前登录用户信息等；
* @author  shenlx
 */
public class ContextHolder {
	/**
	 * SpringMvc下获取request
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;

	}
	/**
	 * SpringMvc下获取session
	 * 
	 * @return
	 */
	public static HttpSession getSession() {
		HttpSession session = getRequest().getSession();
		return session;

	}

	/**
	 * 获取当前登录用户
	 * @return
     */
	public static final StudyUser getSessionUser() {
		HttpSession session = ContextHolder.getSession();
		StudyUser u = (StudyUser) session.getAttribute(ParamConstants.LOCAL_CLINET_USER);
		return u;
	}
	
	/**
	 * 获取当前登录的学生
	 * @return
     */
	public static final StudyStudent getSessionStudent() {
		HttpSession session = ContextHolder.getSession();
		StudyStudent student = (StudyStudent) session.getAttribute(ParamConstants.LOCAL_CLINET_STUDENT);
		return student;
	}
	
	/**
	 * 获取当前登录的老师
	 * @return
     */
	public static final StudyTeacher getSessionTeacher() {
		HttpSession session = ContextHolder.getSession();
		StudyTeacher teacher = (StudyTeacher) session.getAttribute(ParamConstants.LOCAL_CLINET_TEACHER);
		return teacher;
	}
	
//	/**
//	 * 获取当前登录科室
//	 */
//	public static final SysDeptEntity getSessionDept() {
//		HttpSession session = ContextHolder.getSession();
//		SysDeptEntity dept = (SysDeptEntity) session.getAttribute(ParamConstants.LOCAL_CLINET_DEPT);
//		return dept;
//	}
}
